<?php
class CdeUnitsController extends AppController {
	var $name = 'CdeUnits';
	var $helpers = array('Html', 'Form');

	// Index unit - List all units - DONE
	function index() {
		$this->CdeUnit->recursive = 0;
		$this->set('cdeUnits', $this->paginate());
	}

	// Manage unit - DONE
	function manage($id = null) {
		// Check if the unit ID is provided, if not display error message and redirect to index
		if (!$id) {
			$this->Session->setFlash(__('Unit ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}
		
		// Get the unit by ID
		$cdeUnit = $this->CdeUnit->read(null, $id);
		if (!$cdeUnit) {
			// If the unit is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid unit ID, cannot manage', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		} else {
			// Set the value to display
			$this->set('cdeUnit', $cdeUnit);
			$this->set('cdeModules', $this->CdeUnit->CdeModule->find('all', array('conditions' => array('CdeModule.cde_unit_id' => $cdeUnit['CdeUnit']['id']), 'order' => array('CdeModule.id'))));
		}
	}

	// Create new unit - DONE
	function add() {
		if (!empty($this->data)) {
			$this->CdeUnit->create();
			// Check if the unit with the title is already exist
			if ($this->CdeUnit->find('all', array('conditions' => array('title' => $this->data['CdeUnit']['title'])))) {
				$this->Session->setFlash(__('Unit with title "'.$this->data['CdeUnit']['title'].'" is already exist, please enter again', true), 'default', array('class' => 'error-message'));
			} else {
				if ($this->CdeUnit->save($this->data)) {
					// Create new unit successfully, redirect to index
					$this->Session->setFlash(__('The new unit has been successfully created', true), 'default', array('class' => 'success-message'));
					$this->redirect(array('action'=>'index'));
				}
				
				// If it can reach here, error occurred, cannot create new unit
				$this->Session->setFlash(__('An error occurred, new unit could not be successfully created. Please try again!', true), 'default', array('class' => 'error-message'));
			}
		}
	}

	// Update unit - DONE
	function edit($id = null) {
		// Check if the unit ID is provided, if not display error message and redirect to index
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Unit ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}

		// Get the unit by ID
		$cdeUnit = $this->CdeUnit->read(null, $id);
		if (!$cdeUnit && empty($this->data)) {
			// If the unit is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid unit ID, cannot edit', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		} else {
			if (!empty($this->data)) {
				// On update action
				// Check if the unit with the title is already exist
				if ($this->CdeUnit->find('all', array('conditions' => array('title' => $this->data['CdeUnit']['title'], 'id !=' => $this->data['CdeUnit']['id'])))) {
					$this->Session->setFlash(__('Unit with title "'.$this->data['CdeUnit']['title'].'" is already exist, please enter again', true), 'default', array('class' => 'error-message'));
				} else {
					if ($this->CdeUnit->save($this->data)) {
							// Update successfully, redirect to index
							$this->Session->setFlash(__('The unit has been successfully updated', true), 'default', array('class' => 'success-message'));
							$this->redirect(array('action'=>'index'));
					} else {
						// Error occurred, cannot update
						$this->Session->setFlash(__('The unit could not be updated. Please try again!', true), 'default', array('class' => 'error-message'));
					}
				}
			} else {
				// Set the data to update
				$this->data = $cdeUnit;
			}
		}
	}

	// Delete unit - DONE
	function delete($id = null) {
		// Check if the unit ID is provided, if not display error message and redirect to index
		if (!$id) {
			$this->Session->setFlash(__('Unit ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}

		// Get the unit by ID
		$cdeUnit = $this->CdeUnit->read(null, $id);
		if ($cdeUnit != null) {
			// If the unit is found, delete it
			if ($this->CdeUnit->del($id)) {
				// Delete successfully, redirect to index
				$this->Session->setFlash(__('The unit has been successfully deleted', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'index'));
			} else {
				// Error occurred, cannot delete, redirect to index
				$this->Session->setFlash(__('An error occurred, cannot delete the unit. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'index'));
			}
		} else {
			// If the unit is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid unit ID, cannot delete', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}
	}

	// Disable unit - DONE
	function disable($id = null) {
		// Check if the unit ID is provided, if not display error message and redirect to index
		if (!$id) {
			$this->Session->setFlash(__('Unit ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}

		// Get the unit by ID
		$cdeUnit = $this->CdeUnit->read(null, $id);
		if ($cdeUnit != null) {
			// If the unit is found, disable it
			$this->CdeUnit->id = $id;
			if ($this->CdeUnit->saveField('enabled', '0', true)) {
				// Disable successfully, redirect to index
				$this->Session->setFlash(__('The unit has been successfully disabled', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'index'));
			} else {
				// Error occurred, cannot disable, redirect to index
				$this->Session->setFlash(__('An error occurred, cannot disable the unit. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'index'));
			}
		} else {
			// If the unit is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid unit ID, cannot disable', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}
	}
	
	// Enable unit - DONE
	function enable($id = null) {
		// Check if the unit ID is provided, if not display error message and redirect to index
		if (!$id) {
			$this->Session->setFlash(__('Unit ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}

		// Get the unit by ID
		$cdeUnit = $this->CdeUnit->read(null, $id);
		if ($cdeUnit != null) {
			// If the unit is found, enable it
			$this->CdeUnit->id = $id;
			if ($this->CdeUnit->saveField('enabled', '1', true)) {
				// Enable successfully, redirect to index
				$this->Session->setFlash(__('The unit has been successfully enabled', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'index'));
			} else {
				// Error occurred, cannot enable, redirect to index
				$this->Session->setFlash(__('An error occurred, cannot enable the unit. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'index'));
			}
		} else {
			// If the unit is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid unit ID, cannot enable', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}
	}
}
?>